package com.hongdee.market.grievance.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongdee.common.util.StringUtil;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.market.grievance.mapper.ITMarketWhineMapper;
import com.hongdee.market.grievance.pojo.TMarketWhineApprovalPojo;
import com.hongdee.market.grievance.pojo.TMarketWhinePojo;
import com.hongdee.market.grievance.service.ITMarketWhineApprovalService;
import com.hongdee.market.grievance.service.ITMarketWhineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Liu XiaoLong
 * Date: 2017/10/27.
 * Remark:
 */
@Service
public class TMarketWhineServiceImpl implements ITMarketWhineService {

    @Autowired
    private ITMarketWhineMapper itMarketWhineMapper;
    @Autowired
    private ITMarketWhineApprovalService itMarketWhineApprovalService;
    // 查所有
    private String WEBMASTERID = "402881895bb23f8c015bb241e9890000"; // 站长ID
    private String KFMANAGERID = "402881895bb23f8c015bb2437ca90008"; // 客服经理
    // 只查自己
    private String FWMANAGERID = "ff8081815dc56f85015dcf1ac4d84602"; // 服务经理
    private String BJMANAGERID = "4028b8815f6d49cc015f6d59da720000"; // 钣金经理
    private String PQMANAGERID = "4028b8815f6d49cc015f6d5a7b2e0002"; // 喷漆经理
    private String KF = "4028b8815f6d49cc015f6d5b899d0004"; // 客服

    private int currentStatus = 0; // 当前状态


    /**
     * @Name: 分页查询
     * @Author: Liu XiaoLong
     * @Date: 2017/10/27 15:24
     * @Remark:
     */
    @Override
    public Map page(String uid,Map data, int page, int limit) throws Exception {
        Map map = TokenUtil.getRole(uid);
        PageHelper.startPage(page,limit);
        String node = "";
        /**如果是站长或者客服经理 查询所有*/
        if(map.containsKey(WEBMASTERID) || map.containsKey(KFMANAGERID)){ // 是否存在key所对应的value
            node = "all";
        }

        if(!node.equals("all")){
            /**如果是客服 或 sa经理（喷漆、钣金、服务） 查询自己的*/
            if(map.containsKey(FWMANAGERID)) { // sa 服务经理
                node = "2";
            }else if ( map.containsKey(BJMANAGERID)){ // sa 钣金经理
                node = "3";
            }else if ( map.containsKey(PQMANAGERID)){ // sa 喷漆经理
                node = "4";
            }else if (map.containsKey(KF)){ // 客服
                node = "0";
            }
            else {
                throw new Exception("您的角色有误！");
            }
            /*if(map.containsKey(FWMANAGERID) || map.containsKey(BJMANAGERID) || map.containsKey(PQMANAGERID)
                    || map.containsKey(KF)){ // 是否存在key所对应的value
                sa = uid;
            }*/
        }

        List list = itMarketWhineMapper.list(node,data);
        // StringUtil.UUID(); // 随机生成ID
        PageInfo<TMarketWhinePojo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();

        Map resMap = new HashMap();
        resMap.put("dataList",list);
        resMap.put("total",total);
        return resMap;
    }

    /**
     * @Name: 提交审批
     * @Author: Liu XiaoLong
     * @Date: 2017/10/30 13:52
     * @Remark:
     */
    @Override
    public Map submitApprove(String uid, String id, String status,String nextNode,String remark) throws Exception {
        if(id.isEmpty()){
            throw new Exception("id为空，修改失败！");
        }
        /*if(Integer.parseInt(status) > 5){ // 任务已经完成
           throw new Exception("此任务已完成！");
        }*/
        TMarketWhineApprovalPojo tMarketWhineApprovalPojo = new TMarketWhineApprovalPojo();
        Map map = TokenUtil.getRole(uid);
        if(map.containsKey(KFMANAGERID) && status.equals("1")){ //当前为客服经理
            // 2.3.4
            status = nextNode;
            itMarketWhineMapper.updateNode(status,id);

            /**审批记录写入*/
            tMarketWhineApprovalPojo.setApprovalContent(remark);
            tMarketWhineApprovalPojo.setApprovalPeople(uid);
            tMarketWhineApprovalPojo.setWhineId(id);
            tMarketWhineApprovalPojo.setStatus("0");
            SimpleDateFormat spf = new SimpleDateFormat("YYYY-MM-dd");
            tMarketWhineApprovalPojo.setApprovalTime(spf.format(new Date()));
            itMarketWhineApprovalService.save(tMarketWhineApprovalPojo);
        }
        else if((map.containsKey(FWMANAGERID) && status.equals("2")) // 服务
            || (map.containsKey(BJMANAGERID) && status.equals("3")) // 钣金
            || (map.containsKey(PQMANAGERID) && status.equals("4")))// 喷漆
        {
            status = "5";
            itMarketWhineMapper.updateNode(status,id);

            /**审批记录写入*/
            tMarketWhineApprovalPojo.setApprovalContent(remark);
            tMarketWhineApprovalPojo.setApprovalPeople(uid);
            tMarketWhineApprovalPojo.setWhineId(id);
            tMarketWhineApprovalPojo.setStatus("0");
            SimpleDateFormat spf = new SimpleDateFormat("YYYY-MM-dd");
            tMarketWhineApprovalPojo.setApprovalTime(spf.format(new Date()));
            itMarketWhineApprovalService.save(tMarketWhineApprovalPojo);
        }
        else if (map.containsKey(WEBMASTERID) && status.equals("5")){ // 站长
            status = "1";
            itMarketWhineMapper.updateNode(status,id);

            /**审批记录写入*/
            tMarketWhineApprovalPojo.setApprovalContent(remark);
            tMarketWhineApprovalPojo.setApprovalPeople(uid);
            tMarketWhineApprovalPojo.setWhineId(id);
            tMarketWhineApprovalPojo.setStatus("0");
            SimpleDateFormat spf = new SimpleDateFormat("YYYY-MM-dd");
            tMarketWhineApprovalPojo.setApprovalTime(spf.format(new Date()));
            itMarketWhineApprovalService.save(tMarketWhineApprovalPojo);
        }
        else {
            throw new Exception("您无权提交此项目！");
        }


        return new HashMap();
    }

    /**
     * @Name: 完成任务
     * @Author: Liu XiaoLong
     * @Date: 2017/10/30 16:29
     * @Remark:
     */
    @Override
    public Map completeApprove(String uid, String id, String status,String remark) throws Exception {
        Map map = TokenUtil.getRole(uid);

        if(map.containsKey(KFMANAGERID)){ //当前为客服经理
            status = "1"; // 0:未完成   1:已完成
            itMarketWhineMapper.updateStatus(status,id);

            /**审批记录写入*/
            TMarketWhineApprovalPojo tMarketWhineApprovalPojo = new TMarketWhineApprovalPojo();
            tMarketWhineApprovalPojo.setApprovalContent(remark);
            tMarketWhineApprovalPojo.setApprovalPeople(uid);
            tMarketWhineApprovalPojo.setWhineId(id);
            tMarketWhineApprovalPojo.setStatus(status);
            SimpleDateFormat spf = new SimpleDateFormat("YYYY-MM-dd");
            tMarketWhineApprovalPojo.setApprovalTime(spf.format(new Date()));
            itMarketWhineApprovalService.save(tMarketWhineApprovalPojo);
        }
        else {
            throw new Exception("您无权操作！");
        }
        return new HashMap();
    }

    /**
     * @Name: 类型调整
     * @Author: Liu XiaoLong
     * @Date: 2017/10/30 17:14
     * @Remark:
     */
    @Override
    public Map typeAdjust(String uid, String id, String type, String degree) throws Exception {
        Map map = TokenUtil.getRole(uid);
        if(map.containsKey(KFMANAGERID)){ //当前为客服经理
           itMarketWhineMapper.updateDegree(degree,type,id);
        }
        else {
            throw new Exception("您无权操作！");
        }
        return new HashMap();
    }

    /**
     * @Name: 详情
     * @Author: Liu XiaoLong
     * @Date: 2017/10/30 19:48
     * @Remark:
     */
    @Override
    public Map info(String uid, String id) throws Exception {
        Map map = new HashMap();
        map.put("data",itMarketWhineMapper.Info(id));
        return map;
    }

    /**
     * @Name: 是否为客服经理 用于前端是否显示处理部门
     * @Author: Liu XiaoLong
     * @Date: 2017/10/31 10:52
     * @Remark:
     */
    @Override
    public Map isKFOrNot(String uid, String id,String node) throws Exception {
        Map map = TokenUtil.getRole(uid);
        Map respMap = new HashMap();
        if(map.containsKey(KFMANAGERID) && node.equals("1")){ //当前为客服经理
            respMap.put("isKF",false);
            respMap.put("data","");
            return respMap;
        }
        else if((map.containsKey(FWMANAGERID) && node.equals("2")) // 服务
                || (map.containsKey(BJMANAGERID) && node.equals("3")) // 钣金
                || (map.containsKey(PQMANAGERID) && node.equals("4")))// 喷漆
        {
            respMap.put("isKF",true);
            respMap.put("data","5");
            return respMap;
        }
        else if (map.containsKey(WEBMASTERID) && node.equals("5")) { // 站长
            respMap.put("isKF",true);
            respMap.put("data","1");
            return respMap;
        }
        else {
            throw new Exception("您无权提交此项目！");
        }

    }

    /**
     * @Name: 角色判断
     * @Author: Liu XiaoLong
     * @Date: 2017/10/31 15:29
     * @Remark:
     */
    @Override
    public Map role(String uid) throws Exception {
        Map map = TokenUtil.getRole(uid);
        Map respMap = new HashMap();
        if(map.containsKey(KFMANAGERID)){ //当前为客服经理
            respMap.put("role","KFMANAGERID");
            return respMap;
        }
        else if(map.containsKey(FWMANAGERID) // 服务
                || (map.containsKey(BJMANAGERID) ) // 钣金
                || (map.containsKey(PQMANAGERID) ))// 喷漆
        {
            respMap.put("role","SA");
            return respMap;
        }
        else if (map.containsKey(WEBMASTERID)) { // 站长
            respMap.put("role","WEBMASTERID");
            return respMap;
        }
        else if (map.containsKey(KF)) { // 客服
            respMap.put("role","KF");
            return respMap;
        }
        else {
            respMap.put("role","null");
            return respMap;
        }
    }
}
